﻿using System;
using System.Collections.Generic;
using System.Text;
using OABackground.Entities;
using Microsoft.EntityFrameworkCore;
using OABackground.Infrastructure;

namespace OABackground.Services
{
    public class AwardPunish
    {
        public int id;
        public string Sid;
        public string title;
        public string content;
        public string studentName;
        public string sex;
        public string studentClass;
        public string grade;
        public string punishContent;
        public string awardContent;
        public string state;
        public string issue;
        public string applyTime;
        public string checkTime;
    }
    public class AwardPunishService
    {


        private AwardPunishRepository awardPunishRepository = new AwardPunishRepository();
        /// <summary>
        /// 提交奖惩
        /// </summary>
        /// <param name="Sid"></param>
        /// <param name="Content"></param>
        /// <param name="PunishContent"></param>
        /// <param name="AwardContent"></param>
        /// <returns></returns>
        public int AddAwardPunishlist(string Title, string Sid, int PunishContent, string AwardContent, string Content)
        {

            var student = awardPunishRepository.QueryStudentById(Sid);
            //var studentInfo = new Student
            //{
            //    Id = Sid,
            //    Name = student.Name,
            //    Class = student.Class,
            //    Grade = student.Grade,
            //    Dormitory = student.Dormitory,
            //    SelfPhone = student.SelfPhone,
            //    ParentPhone = student.ParentPhone
            //};
            if (student == null)
                return -1;
            var AwardPinishInfo = new Awardpunish
            {
                Sid = Sid,
                Title = Title,
                PunishContent = PunishContent,
                AwardContent = AwardContent,
                Content = Content,
                ApplyTime = DateTime.Now.ToLocalTime(),
                State = 0,
                Issue = "无",
                //    S = studentInfo
            };
            return (AddAwardPunish(AwardPinishInfo));
        }

        /// <summary>
        /// 添加新奖惩表表项
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddAwardPunish(Awardpunish model)//添加新表项
        {
            int AP = 0;
            using (var dbConnect = new oaContext())
            {
                //Check here.
                if (model.Title == null)
                {
                    AP = 0;
                }
                else if (model.AwardContent == null)
                    AP = 2;
                else if (model.PunishContent == -1)
                    AP = 3;
                else
                {
                    var repo = new AwardPunishRepository();
                    repo.Add(model);
                    AP = 1;
                }
            }

            return (AP);
        }

        /// <summary>
        /// 查询全部奖惩
        /// </summary>
        /// <returns></returns>
        public List<AwardPunish> QueryAwardPunishAll()
        {
            var list = awardPunishRepository.QueryAll();
            List<AwardPunish> awardpunishList = new List<AwardPunish>();
            for (int i = 0; i < list.Count; i++)
            {
                var student = awardPunishRepository.QueryStudentById(list[i].Sid);
                if (student.Equals(""))
                    continue;
                var awardpunish = new AwardPunish
                {
                    id = list[i].Id,
                    Sid = list[i].Sid,
                    issue = list[i].Issue,
                    content = list[i].Content,
                    studentName = student.Name,
                    studentClass = student.Class,
                    grade = student.Grade,
                    sex = student.Sex,
                    title = list[i].Title,
                    awardContent = list[i].AwardContent,
                    applyTime = list[i].ApplyTime.ToString().Replace('T', ' '),
                    checkTime = list[i].CheckTime.ToString().Replace('T', ' '),
                };
                switch (list[i].PunishContent)
                {
                    case 0:
                        awardpunish.punishContent = "警告";
                        break;
                    case 1:
                        awardpunish.punishContent = "记过";
                        break;
                    case 2:
                        awardpunish.punishContent = "留校察看";
                        break;
                    case 3:
                        awardpunish.punishContent = "开除学籍";
                        break;
                    default:
                        awardpunish.punishContent = null;
                        break;
                }
                if (list[i].State == 0)
                {
                    awardpunish.state = "等待审核";
                }
                else if (list[i].State == 1)
                {
                    awardpunish.state = "已通过";
                }
                else
                    awardpunish.state = "已拒绝";
                awardpunishList.Add(awardpunish);
            }
            return awardpunishList;
        }
    }
}
